package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.groupon.*;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.GrouponMapper;
import com.cskaoyan.mapper.GrouponRulesMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @program: whyccz-mall-project
 * @description:
 * @create: 2021-06-09 17:32
 **/
@Service
public class GrouponServiceImpl implements  GrouponService {
	@Autowired
	GrouponRulesMapper grouponRulesMapper;
	@Autowired
	GoodsMapper goodsMapper;
	@Autowired
	GrouponMapper grouponMapper;

	@Override
	public GrouponListVO getGrouponList(GrouponReq grouponReq) {
		PageHelper.startPage(grouponReq.getPage(),grouponReq.getLimit());

		GrouponRulesExample grouponRulesExample = new GrouponRulesExample();

		grouponRulesExample.setOrderByClause("add_time desc");
		GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
		criteria.andDeletedEqualTo(false);
		if (grouponReq.getGoodsId()!=null){
			criteria.andGoodsIdEqualTo(grouponReq.getGoodsId());
		}
		List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);

		PageInfo<GrouponRules> grouponRulesPageInfo = new PageInfo<>(grouponRules);
		long total = grouponRulesPageInfo.getTotal();
		GrouponListVO grouponListVO = new GrouponListVO();
		grouponListVO.setTotal(total);
		grouponListVO.setItems(grouponRules);
		return grouponListVO;
	}

	@Override
	public GrouponRules createGroupon(GrouponCreateBO grouponCreateBO) {

		Goods goods = goodsMapper.selectByPrimaryKey(Integer.valueOf(grouponCreateBO.getGoodsId()));
		GrouponRules grouponRules = new GrouponRules();
		Calendar calendar = Calendar.getInstance();

		grouponRules.setAddTime(new Timestamp(calendar.getTimeInMillis()));
		grouponRules.setExpireTime(Timestamp.valueOf(grouponCreateBO.getExpireTime()));
		grouponRules.setPicUrl(goods.getPicUrl());
		grouponRules.setDeleted(false);
		grouponRules.setGoodsId(goods.getId());
		grouponRules.setDiscount(new BigDecimal(grouponCreateBO.getDiscount()));
		grouponRules.setDiscountMember(Integer.valueOf(grouponCreateBO.getDiscountMember()));
		grouponRules.setUpdateTime(new Timestamp(calendar.getTimeInMillis()));
		grouponRules.setGoodsName(goods.getName());
		grouponRulesMapper.insertSelective(grouponRules);

		return grouponRules;
	}

	@Override
	public void updateGroupon(UpdateGrouponBO updateGrouponBO) {
		GrouponRules grouponRules = new GrouponRules();
		Calendar calendar = Calendar.getInstance();
		grouponRules.setId(updateGrouponBO.getId());
		grouponRules.setAddTime(new Timestamp(calendar.getTimeInMillis()));
		grouponRules.setExpireTime(Timestamp.valueOf(updateGrouponBO.getExpireTime()));
		grouponRules.setPicUrl(updateGrouponBO.getPicUrl());
		grouponRules.setDeleted(false);
		grouponRules.setGoodsId(Integer.valueOf(updateGrouponBO.getGoodsId()));
		grouponRules.setDiscount(new BigDecimal(updateGrouponBO.getDiscount()));
		grouponRules.setDiscountMember(Integer.valueOf(updateGrouponBO.getDiscountMember()));
		grouponRules.setUpdateTime(new Timestamp(calendar.getTimeInMillis()));
		grouponRules.setGoodsName(updateGrouponBO.getGoodsName());
		grouponRulesMapper.updateByPrimaryKeySelective(grouponRules);
	}

	@Override
	public void deleteGroupon(UpdateGrouponBO updateGrouponBO) {

		grouponRulesMapper.deleteById(updateGrouponBO.getId());

	}

	@Override
	public GrouponListRecordVO getGrouponlistRecord(BaseReq baseReq) {
		GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
		grouponRulesExample.createCriteria().andDeletedEqualTo(false);
		List<GrouponRules> grouponRulesList = grouponRulesMapper.selectByExample(grouponRulesExample);

		PageHelper.startPage(baseReq.getPage(),baseReq.getLimit());

		GrouponExample grouponExample = new GrouponExample();
		grouponExample.createCriteria().andDeletedEqualTo(false);
		List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
		PageInfo<Groupon> pageInfo = new PageInfo<>(groupons);
		long total = pageInfo.getTotal();
		List<GrouponRecordBean> grouponRecordBeanList=new ArrayList<>();
		for (Groupon groupon : groupons) {
			GrouponRecordBean grouponRecordBean = new GrouponRecordBean();

			ItemGrouponVO itemGrouponVO = new ItemGrouponVO();
			itemGrouponVO.setOrderId(groupon.getOrderId());
			itemGrouponVO.setUserId(groupon.getUserId());
			ItemRulesVO itemRulesVO = new ItemRulesVO();
			itemRulesVO.setAddTime(LocalDateTime.now());
			ItemSubGrouponsVO itemSubGrouponsVO = new ItemSubGrouponsVO();
			itemSubGrouponsVO.setLength(1);
			String iconUrl=null;
			for (GrouponRules grouponRules : grouponRulesList) {
				if (grouponRules.getId().equals(groupon.getRulesId())){

					itemRulesVO.setDiscount(grouponRules.getDiscount().toString());
					itemRulesVO.setDiscountMember(grouponRules.getDiscountMember());
					itemRulesVO.setExpireTime(grouponRules.getExpireTime().toLocalDateTime());
					iconUrl=grouponRules.getPicUrl();
				}

			}
			grouponRecordBean.setGroupon(itemGrouponVO);
			grouponRecordBean.setIconUrl(iconUrl);
			grouponRecordBean.setRules(itemRulesVO);
			grouponRecordBean.setSubGroupons(itemSubGrouponsVO);
			grouponRecordBeanList.add(grouponRecordBean);
		}
		GrouponListRecordVO grouponListRecordVO = new GrouponListRecordVO();

		grouponListRecordVO.setTotal(total);
		grouponListRecordVO.setItems(grouponRecordBeanList);
		return grouponListRecordVO;
	}


}
